package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.define.UserDefine;
import com.example.demo.impl.DahuaVideoService;
import com.example.demo.service.VideoService;
import com.example.demo.utils.SignVideoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException;

/**
 * @author xumf
 * @date 2019/11/25 16:08
 */
@Controller
public class IndexController {

    @Autowired
    private DahuaVideoService dahuaVideoService;

    @Autowired
    private VideoService videoService;


    /**
     * 大华视频对接
     * @return
     * @throws NoSuchAlgorithmException
     */
    @RequestMapping("getVideoToken")
    @ResponseBody
    public String dahuaVideo() throws NoSuchAlgorithmException {
        return getToken();
    }

    @RequestMapping("getVideoUrl")
    @ResponseBody
    public String getVideoUri(HttpServletRequest request) throws NoSuchAlgorithmException {
        String channelId=request.getParameter("sn");
        if(StringUtils.isEmpty(channelId)){
            JSONObject object=new JSONObject();
            object.put("code",999);
            object.put("msg","sn参数不能为空");
            return object.toString();
        }
        String token=getToken();
        String resutl= dahuaVideoService.getRealMonitor(channelId,token);
        if(resutl==null){
            JSONObject object=new JSONObject();
            object.put("code",999);
            object.put("msg","没有获取到信息");
            return object.toString();
        }
        return resutl;
    }

    private String getToken() throws NoSuchAlgorithmException {
         return videoService.getToken();
    }

    private String keepAlive(String token){
        String result=dahuaVideoService.keepAlive(token);
        return result;
    }

}
